package com.runa.monitor.platinterface.mqtt;


//import com.alibaba.cloud.nacos.NacosDiscoveryProperties;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 初始化创建mqtt连接客户端
 */
@Slf4j
@Order(998)
@Service
public class MqttInitService implements CommandLineRunner {

    @Value("${server.port}")
    private String port;

    @Value("${dispatch.grpc.local.ip:}")
    private String localIp;

    @Resource
    private MQTTExecutor mqttExecutor;
    @Resource
    private NacosDiscoveryProperties nacosDiscoveryProperties;

    @Override
    public void run(String... args) throws Exception {
        createMqttConnect();
    }

    private void createMqttConnect() {
        try {
            //创建mqtt客户端连接
            MQTTConnector mqttConnector = new MQTTConnector("business_comm_system_" + nacosDiscoveryProperties.getIp() + "_" + port, mqttExecutor);
//            MQTTConnector mqttConnector = new MQTTConnector("business_comm_system" + "_" + localIp + "_" + port+ "_" +new Random().nextInt(100), bpConsumer);
            MqttBaseController.setConnector("business_comm_system_", mqttConnector);
        } catch (Exception e) {
            log.error("通讯创建mqtt连接失败,{}", e.toString());
        }
        log.info("通讯创建mqtt连接完成！");
    }

    public static List<String> getTopicsList() {
        //订阅网关上行数据、mqtt客户端上下线状态
        List<String> topics = new ArrayList<>();
//        topics.add("$share///aigw/+/up");
        topics.add("/aigw/+/up");
        topics.add("/aigw_jy/+/up");
        topics.add("/aigw_mt/+/up");
        // 黑蚂蚁
        topics.add("/aigw_hmy/+/up");
        topics.add("/RU-VC-05/+/up");
        topics.add("/RU-METER/+/up");
        topics.add("/hotmeter-00/+/up");
        topics.add("/watermeter-00/+/up");
        topics.add("/RU-LDTU-01/+/up");
//        topics.add("/RU-VC-05/+/up");
        // /aigw_厂家/xxxxxxxx/down   新厂家格式
        // 4G面板
        topics.add("/RU-STC-BT1/+/up");
        topics.add("/RU-LRTU-01/+/up");
        topics.add("/RU-LRTU-01A/+/up");

        topics.add("/HM-1/+/up");
        topics.add("/WM-1/+/up");
        topics.add("/VCHM-1/+/up");
        topics.add("/VCWM-1/+/up");

//        }
        topics.add("$SYS/brokers/+/clients/+/disconnected");
        return topics;
    }
}
